package org.rslai.util.string;

/**
 * 验证类
 * @author rslai
 */
public class Verify {
	private static Verify instance = new Verify();

	/**
	 * 返回一个 Verify 实例
	 * @return Verify 实例
	 */
	static public Verify getInstance() {
		return instance;
	}

	/**
	 * 私有化构造函数
	 */
	private Verify() {
		
	}

	/**
	 * 判断是否为数字 <br>
	 * 支持格式: <br>
	 * "33" "+33" "033.30" "-.33" ".33" " 33." " 000.000 "
	 * @param str String
	 * @return boolean
	 */
	public boolean isNumeric(String str) {
		int begin = 0;
		boolean once = true;
		if (str == null || str.trim().equals("")) {
			return false;
		}
		str = str.trim();
		if (str.startsWith("+") || str.startsWith("-")) {
			if (str.length() == 1) {
				// "+" "-"
				return false;
			}
			begin = 1;
		}
		for (int i = begin; i < str.length(); i++) {
			if (!Character.isDigit(str.charAt(i))) {
				if (str.charAt(i) == '.' && once) {
					// '.' can only once
					once = false;
				} else {
					return false;
				}
			}
		}
		if (str.length() == (begin + 1) && !once) {
			// "." "+." "-."
			return false;
		}
		return true;
	}

	/**
	 * 判断是否为整数 <br>
	 * 支持格式:<br>
	 * "33" "003300" "+33" " -0000 "
	 * @param str String
	 * @return boolean
	 */
	public boolean isInteger(String str) {
		int begin = 0;
		if (str == null || str.trim().equals("")) {
			return false;
		}
		str = str.trim();
		if (str.startsWith("+") || str.startsWith("-")) {
			if (str.length() == 1) {
				// "+" "-"
				return false;
			}
			begin = 1;
		}
		for (int i = begin; i < str.length(); i++) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

}
